/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.entities.model.DTO.TurmaDTO;
import app.view.classes.ViewProfessor;
import app.view.classes.ViewTurma;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class LancarChamada extends javax.swing.JFrame {

    private String codigoMatricula;
    private TurmaDTO turma;
    private ArrayList<TurmaDTO> listaTurmasProfessor;
    private ViewProfessor viewProfessor;
    private ViewTurma viewTurma;

    public LancarChamada(String CodigoMatricula) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width / 2) - (getWidth() / 2), (screenSize.height / 2) - (getHeight() / 2));
        this.codigoMatricula = CodigoMatricula;
        this.viewProfessor = new ViewProfessor();
        this.viewTurma = new ViewTurma();
        this.turma = new TurmaDTO();
        this.dtChamada.setDate(new Date());
        carregarTurmasProfessor();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblChamada = new javax.swing.JTable();
        btnLancarFaltas = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnSelecionarTurma = new javax.swing.JButton();
        cbMaterias = new javax.swing.JComboBox();
        dtChamada = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chamada Eletrônica");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chamada"));

        tblChamada.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Matrícula", "Nome", "Sobrenome", "Presente"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblChamada);

        btnLancarFaltas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/images.jpg"))); // NOI18N
        btnLancarFaltas.setText("Lançar Faltas");
        btnLancarFaltas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLancarFaltasActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/sair.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(btnLancarFaltas)
                .addGap(37, 37, 37)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLancarFaltas)
                    .addComponent(btnCancelar)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Turma"));

        jLabel1.setText("Matéria");

        btnSelecionarTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/download.jpg"))); // NOI18N
        btnSelecionarTurma.setText("Exibir Alunos");
        btnSelecionarTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionarTurmaActionPerformed(evt);
            }
        });

        jLabel2.setText("Data");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(40, 40, 40)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cbMaterias, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(dtChamada, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(29, 29, 29)
                        .addComponent(btnSelecionarTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbMaterias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(dtChamada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(btnSelecionarTurma)))
                .addGap(140, 140, 140))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelecionarTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionarTurmaActionPerformed
        if (this.cbMaterias.getSelectedIndex() != -1) {
            DefaultTableModel tblModel = (DefaultTableModel) this.tblChamada.getModel();
            this.turma = this.listaTurmasProfessor.get(this.cbMaterias.getSelectedIndex());
            for (MatriculaAlunoDTO matAluno : this.turma.getAlunos()) {
                tblModel.addRow(new Object[]{matAluno.getCodigoMatricula(), matAluno.getAluno().getNome(),
                            matAluno.getAluno().getSobrenome(), true});
            }
        }


    }//GEN-LAST:event_btnSelecionarTurmaActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLancarFaltasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLancarFaltasActionPerformed
        if(tblChamada.getRowCount() <= 0){
            JOptionPane.showMessageDialog(this, "Atenção! Essa turma não tem nenhum aluno matriculado",
                    "Lançar Chamada", JOptionPane.INFORMATION_MESSAGE);
            return;
        }else{
            ArrayList<MatriculaAlunoDTO> alunosPresentes = new ArrayList<>();
            DefaultTableModel tblModel = (DefaultTableModel) this.tblChamada.getModel();
            for(int i = 0; i < tblChamada.getRowCount(); i++){
                String Matricula = String.valueOf(((DefaultTableModel) this.tblChamada.getModel()).getValueAt(i, 0));
                if(this.tblChamada.getModel().getValueAt(i, 3) == true){
                    for(MatriculaAlunoDTO matAluno : this.turma.getAlunos()){
                        if(matAluno.getCodigoMatricula().equals(Matricula)){
                            alunosPresentes.add(matAluno);
                        }
                    }
                }
            }
            this.viewTurma.lancarChamada(turma, alunosPresentes, this.dtChamada.getDate());
        }
    }//GEN-LAST:event_btnLancarFaltasActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnLancarFaltas;
    private javax.swing.JButton btnSelecionarTurma;
    private javax.swing.JComboBox cbMaterias;
    private com.toedter.calendar.JDateChooser dtChamada;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblChamada;
    // End of variables declaration//GEN-END:variables

    public void carregarTurmasProfessor() {
        this.listaTurmasProfessor = new ArrayList<>();
        listaTurmasProfessor = this.viewTurma.retornarListaTurmas(codigoMatricula);
        DefaultComboBoxModel cbModel = (DefaultComboBoxModel) this.cbMaterias.getModel();
        for (TurmaDTO turma : listaTurmasProfessor) {
            cbModel.addElement(turma.getNomeMateria());
        }
    }
}
